package com.newlink.building.common_base.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.newlink.building.common_base.constant.Base_Constant
import com.newlink.building.common_base.event.Base_NetworkChangeEvent
import com.newlink.building.common_base.model.bean.ProjectData
import com.newlink.building.common_base.model.bean.SipInfo
import com.newlink.building.common_base.model.bean.UserInfoData
import com.newlink.building.common_base.model.bean.UserInfoResp
import com.newlink.building.common_base.utils.Base_Preference
import com.newlink.building.common_base.widget.fragment.VisibilityFragment
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Created by xiaodong on 2022/7/25.
 */
abstract class BaseFragment : VisibilityFragment() {

    /**
     * token
     */
    protected var accessToken: String by Base_Preference(Base_Constant.TOKEN_KEY, "")

    /**
     * kcp userId
     */
    protected var cUserId: String by Base_Preference(Base_Constant.KCP_USER_ID_KEY, "")

    /**
     * userId
     */
    protected var userId: Long by Base_Preference(Base_Constant.USER_ID_KEY, 0L)

//    /**
//     * prjId 项目id
//     */
//    protected var prjId: String by Preference(Constant.PRJ_ID_KEY, "")

    /**
     * 声网Id
     */
    protected var agoraId: Int by Base_Preference(Base_Constant.AGORA_ID_KEY, 0)

    protected var agoraAppId: String by Base_Preference(Base_Constant.AGORA_App_ID_KEY, "")

    /**
     * server type
     */
    protected var serverType: Int by Base_Preference(Base_Constant.KCP_SERVER_TYPE_KEY, 0)

    /**
     * 用户详情
     */
    protected var userInfoData: UserInfoData by Base_Preference(
        Base_Constant.USER_INFO_KEY,
        UserInfoData()
    )

    protected var userInfoResp: UserInfoResp by Base_Preference(
        Base_Constant.USER_INFO_RESP_KEY,
        UserInfoResp()
    )

    protected var sipInforData: SipInfo by Base_Preference(
        Base_Constant.SIP_INFO_KEY,
        SipInfo()
    )

    protected var countryCode: String by Base_Preference(Base_Constant.COUNTRY_CODE_KEY, "")


    /**
     * check login
     */
    protected var isLogin: Boolean by Base_Preference(Base_Constant.LOGIN_KEY, false)

    /**
     * 检测是否为主账号
     */
    protected var isPrimaryAccount: Boolean by Base_Preference(Base_Constant.PRIMARY_ACCOUNT_KEY, false)

    /**
     * 登录手机号
     */
    protected var phoneNumber: String by Base_Preference(Base_Constant.USERNAME_KEY, "")

    /**
     * SIP线路
     */
    protected var sipLine: String by Base_Preference(Base_Constant.SIP_LINE, "")

    /**
     * 缓存上一次的网络状态
     */
    protected var hasNetwork: Boolean by Base_Preference(Base_Constant.HAS_NETWORK_KEY, true)

    /**
     * 项目id
     */
    protected var projectId: String by Base_Preference(Base_Constant.PRJ_ID_KEY, "")

    /**
     * 选择的项目数据
     */
    protected var projectData: ProjectData by Base_Preference(Base_Constant.PRJ_DATA_KEY, ProjectData())

    /**
     * 视图是否加载完毕
     */
    private var isViewPrepare = false

    /**
     * 数据是否加载过了
     */
    private var hasLoadData = false

    /**
     * 加载布局
     */
//    @LayoutRes
    abstract fun attachLayoutRes(): View

    /**
     * 初始化 View
     */
    abstract fun initView(view: View)
    open fun initData() {}

    /**
     * 懒加载
     */
    abstract fun lazyLoad()

    /**
     * 是否使用 EventBus
     */
    open fun useEventBus(): Boolean = true

    /**
     * 无网状态—>有网状态 的自动重连操作，子类可重写该方法
     */
    open fun doReConnected() {
//        lazyLoad()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return attachLayoutRes()
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            lazyLoadDataIfPrepared()
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (useEventBus()) {
            EventBus.getDefault().register(this)
        }
        isViewPrepare = true
        initData()
        initView(view)
        lazyLoadDataIfPrepared()
    }

    open fun lazyLoadDataIfPrepared() {
        if (userVisibleHint && isViewPrepare && !hasLoadData) {
            lazyLoad()
            hasLoadData = true
        }
    }

    open val mRetryClickListener: View.OnClickListener = View.OnClickListener {
        lazyLoad()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onNetworkChangeEvent(event: Base_NetworkChangeEvent) {
        if (event.isConnected) {
            doReConnected()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (useEventBus()) {
            EventBus.getDefault().unregister(this)
        }
    }
}
